/*
 * Create Author  : jinpeng.chen
 * Create Date    : 2017-01-10
 * Project        : springboot-chapter5
 * File Name      : DemoAction.java
 *
 * Copyright (c) 2010-2015 by Shanghai HanTao Information Co., Ltd.
 * All rights reserved.
 *
 */
package com.charles.app.action;

import com.charles.app.dto.PersonDTO;
import com.charles.app.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * 功能描述:  <p>
 *
 * @author : jinpeng.chen <p>
 * @version 1.0 2017-01-10
 * @since springboot-chapter5 1.0
 */
@RestController
@RequestMapping("/demo")
public class DemoAction {

    @Autowired
    private PersonService personService;

    @GetMapping("/show")
    public ResponseEntity<List<PersonDTO>> showPerson(String name) {
        Optional<List<PersonDTO>> byNameLike = personService.findByNameLike(name);
        if (byNameLike.isPresent()) {
            List<PersonDTO> personDTOs = byNameLike.get();
            return new ResponseEntity<List<PersonDTO>>(personDTOs, HttpStatus.OK);
        }
        return new ResponseEntity<List<PersonDTO>>(HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
